<%--
Created by IntelliJ IDEA.
User: wang
Date: 2021/3/16/016
Time: 18:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--添加标签库--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<%--在作用域中保存一个当前路径参数ctx--%>
<c:set value="${pageContext.request.contextPath}" var="ctx"/>
<div class="providerAdd">
    <div class="location">
        <strong>你现在所在的位置是:</strong>
        <span>供应商修改页面</span>
    </div>
    <form action="#" method="post" id="providerUpdateForm">
        <!--div的class 为error是验证错误，ok是验证成功-->
        <input type="hidden" name="id" value="${provider.id}"/>
        <div class="">
            <label for="providerId">供应商编码：</label>
            <input type="text" name="proCode" id="providerId" readonly="readonly" value="${provider.proCode}"/>
            <span id="sp1">*</span>
        </div>
        <div>
            <label for="providerName">供应商名称：</label>
            <input type="text" name="proName" id="providerName" value="${provider.proName}"/>
            <span id="sp2">*</span>
        </div>
        <div>
            <label for="people">联系人：</label>
            <input type="text" name="proContact" id="people" value="${provider.proContact}"/>
            <span id="sp3">*</span>

        </div>
        <div>
            <label for="phone">联系电话：</label>
            <input type="text" name="proPhone" id="phone" value="${provider.proPhone}"/>
            <span id="sp4"></span>
        </div>
        <div>
            <label for="address">联系地址：</label>
            <input type="text" name="proAddress" id="address" value="${provider.proAddress}"/>
            <span id="sp5"></span>

        </div>
        <div>
            <label for="fax">传真：</label>
            <input type="text" name="proFax" id="fax" value="${provider.proFax}"/>
            <span id="sp6"></span>

        </div>
        <div>
            <label for="describe">描述：</label>
            <textarea name="proDesc" rows="8" cols="40" id="describe">${provider.proDesc}</textarea>
            <span id="sp7"></span>

        </div>
        <div class="providerAddBtn">
            <!--<a href="#">保存</a>-->
            <!--<a href="providerList.jsp">返回</a>-->
            <input type="button" value="保存" id="btn"/>
            <input type="button" value="返回" onclick="location.href='${ctx}/provider/providerList'"/>
        </div>
    </form>
</div>
<%--修改供应商--%>
<script type="text/javascript">
    $(function () {
        $("#btn").click(function () {
            //js验证
            var flag1 = false;
            var flag2 = false;
            var flag3 = false;
            var flag4 = false;

            //电话号码正则表达式
            var regexPhone = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;

            if ($("#providerId").val() == null || $("#providerId").val() == "" || $("#providerId").val() == "null") {
                $("#sp1").html("供应商编码不能为空或null").css("color", "red");
            } else if ($("#providerId").val() == "${provider.proCode}") {
                $("#sp1").html("供应商编码未做修改").css("color", "green");
                flag1 = true;
            } else {
                $("#sp1").html("供应商编码符合条件").css("color", "green");
                flag1 = true;
            }

            if ($("#providerName").val() == null || $("#providerName").val() == "" || $("#providerName").val() == "null") {
                $("#sp2").html("供应商名称不能为空或null").css("color", "red");
            } else if ($("#providerName").val() == "${provider.proName}") {
                $("#sp2").html("供应商名称未做修改").css("color", "green");
                flag2 = true;
            } else {
                //获得后端的供应商集合的json数据
                var providerList =${providerList};
                //alert(providerList);
                // alert(JSON.stringify(providerList));
                var flag = false;
                var val0 = $("#providerName").val();
                for (var i in providerList) {
                    // console.log("aaaaaaaaaaaa:"+providerList[i].proCode);
                    if (providerList[i].proName == val0) {
                        flag = true;
                        break;
                    }
                }
                // alert("flag-----" + flag);
                if (flag) {
                    //alert("该供应商编码存在！");
                    $("#sp2").html("供应商名称存在!").css("color", "red");
                } else {
                    //alert("该供应商编码不存在！");
                    $("#sp2").html("供应商名称符合条件").css("color", "green");
                    flag2 = true;
                }

            }

            if ($("#people").val() == null || $("#people").val() == "" || $("#people").val() == "null") {
                $("#sp3").html("联系人不能为空或null").css("color", "red");
            } else {
                $("#sp3").html("联系人符合条件").css("color", "green");
                flag3 = true;
            }


            if ($("#phone").val() == "" || $("#phone").val() == "null") {
                $("#sp4").html("手机号不能为空或null").css("color", "red");
            } else if (!regexPhone.test($("#phone").val())) {
                $("#sp4").html("手机号不符合条件！").css("color", "red");
            } else {
                $("#sp4").html("手机号符合条件").css("color", "green");
                flag4 = true;
            }
            //alert("flag1----" + flag1);
            if (flag1 && flag2 && flag3 && flag4) {
                var data = $("#providerUpdateForm").serialize();
                // alert(data);
                $.post(
                    ctx + "/provider/providerUpdate",
                    data,
                    function (result) {
                        // alert(result);
                        if (result > 0) {
                            alert("修改成功！");
                            location.href = "${ctx}/provider/providerList";
                        } else {
                            alert("修改失败！");
                        }

                    }, "json"
                );
            } else {
                alert("请根据页面提示信息进行修改！");
            }
        });

    })
    ;
</script>
